<?php

/*********************************************
*          CrearConsultasPHPMySQL            *
*                                            *
*                                            *
* ---------------------------                *
* | CrearConsulta.class.php |                *
* ---------------------------                *
* Version 0.1                                *
* Es un proyecto de codigo abierto empezado  *
* por Francisco Wiegand C.                   *
*                                            *
* Distribuido en:                            *
* crearconsultasphpmysql.googlecode.com      *
* ========================================== *
* (c) 2009 Francisco Wiegand C. (Wign)       *
* Todos los derechos reservados              *
* ========================================== *
* This program has been written under the    *
* terms of the GNU General Public Licence as *
* published by the Free Software Foundation. *
*                                            *
*********************************************/

class CrearConsulta {
	
	private $tabla;
	private $campos;
	private $condiciones;
	
	public function __construct($tablaAUtilizar){
		
		$this->tabla = $tablaAUtilizar;
		
	}
	
	public function agnadirCampoDeBusqueda($nuevoCampo){
		
		$this->campos[sizeof($this->campos)-1] = $nuevoCampo;
		
	}
	
	public function agnadirCondicionDeBusqueda($campo, $comparador, $q){
		
		$this->condiciones[sizeof($this->condiciones)-1] = array("campo" => $campo, "comparador" => $comparador, "q" => $q);
		
	}
	
	public function dameConsulta($ORDEN = null){
		
		$condicionFinal;
		$camposAUtilizar;
		$condicionesAUtilizar;
		
		//�Esta vacia la lista de campos?
		
		if($this->campos == null){
			
			$camposAUtilizar = "*";
			
		} else {
			
			$camposAUtilizar = implode(",", $this->campos);
			
		}
		
		$condicionFinal = "SELECT " . $camposAUtilizar . " FROM " . $this->tabla;
		
		if(sizeof($this->condiciones) != 0){
		
			$condicionFinal .= " WHERE";
			
			foreach($this->condiciones as $numCondicion){
				
				
				$condicionFinal .= " " . $numCondicion["campo"] . "" . $numCondicion["comparador"] . "'" . $numCondicion["q"] . "' AND ";
				
			}
			
			//sacamos la �ltima coma
			
			$condicionFinal = substr($condicionFinal, 0, strlen($condicionFinal)-4);
		
		}
		
		if(!($ORDEN == null)){
			
			$condicionFinal .= " ORDER BY " . $ORDEN;
			
		}
		
		return (string) $condicionFinal;
		
	}
	
	public function dameTabla(){
		
		return $this->tabla;
		
	}
	
}